"""
Schedules router for managing room schedules (Admin only).
"""
from typing import List, Optional
from fastapi import APIRouter, Depends, Query, HTTPException, status
from sqlalchemy.orm import Session
from uuid import UUID
from app.database import get_db
from app.schemas.schedule import ScheduleCreate, ScheduleUpdate, ScheduleResponse
from app.services import schedule_service
from app.routers.auth import get_current_user, get_current_admin_user
from app.models.user import User

router = APIRouter()


@router.post("/", response_model=ScheduleResponse, status_code=status.HTTP_201_CREATED)
def create_schedule(
    schedule: ScheduleCreate,
    db: Session = Depends(get_db),
    current_admin: User = Depends(get_current_admin_user)
):
    """
    Create a new room schedule (Admin only).
    """
    db_schedule = schedule_service.create_schedule(db, schedule)
    
    return ScheduleResponse.model_validate(db_schedule)


@router.post("/default/{room_id}", response_model=List[ScheduleResponse])
def create_default_schedule(
    room_id: UUID,
    db: Session = Depends(get_db),
    current_admin: User = Depends(get_current_admin_user)
):
    """
    Create default schedule (Mon-Fri 8:00-18:00) for a room (Admin only).
    """
    schedules = schedule_service.create_default_schedule(db, room_id)
    return [ScheduleResponse.model_validate(schedule) for schedule in schedules]


@router.get("/", response_model=List[ScheduleResponse])
def get_schedules(
    room_id: Optional[UUID] = None,
    skip: int = Query(0, ge=0),
    limit: int = Query(100, ge=1, le=1000),
    db: Session = Depends(get_db)
    # Removed authentication dependency to allow public access
):
    """
    Get all schedules with optional room filter (Public access).
    """
    schedules = schedule_service.get_schedules(
        db, room_id=room_id, skip=skip, limit=limit
    )
    return [ScheduleResponse.model_validate(schedule) for schedule in schedules]


@router.get("/room/{room_id}", response_model=List[ScheduleResponse])
def get_room_schedule(
    room_id: UUID,
    db: Session = Depends(get_db)
    # Removed authentication dependency to allow public access
):
    """
    Get all schedules for a specific room (Public access).
    """
    schedules = schedule_service.get_room_schedule(db, room_id)
    return [ScheduleResponse.model_validate(schedule) for schedule in schedules]


@router.get("/{schedule_id}", response_model=ScheduleResponse)
def get_schedule(
    schedule_id: UUID,
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_user)
):
    """
    Get a specific schedule by ID.
    """
    schedule = schedule_service.get_schedule(db, schedule_id)
    if not schedule:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail="Schedule not found"
        )
    return ScheduleResponse.model_validate(schedule)


@router.put("/{schedule_id}", response_model=ScheduleResponse)
def update_schedule(
    schedule_id: UUID,
    schedule_update: ScheduleUpdate,
    db: Session = Depends(get_db),
    current_admin: User = Depends(get_current_admin_user)
):
    """
    Update schedule information (Admin only).
    """
    updated_schedule = schedule_service.update_schedule(db, schedule_id, schedule_update)
    return ScheduleResponse.model_validate(updated_schedule)


@router.delete("/{schedule_id}")
def delete_schedule(
    schedule_id: UUID,
    db: Session = Depends(get_db),
    current_admin: User = Depends(get_current_admin_user)
):
    """
    Delete a schedule (Admin only).
    """
    schedule_service.delete_schedule(db, schedule_id)
    return {"message": "Schedule deleted successfully"}